package
{
	import flash.filters.GlowFilter;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;

	public class PuzzleListing extends FlxGroup
	{
		public static const LISTING_WIDTH:int = 520;
		public static const LISTING_HEIGHT:int = 60;
		
		private var bg:FlxSprite;
		private var titleText:FlxText;
		private var authorText:FlxText;
		private var overlap:Boolean;
		
		// Format will be:
		// 0-map id
		// 1-playerid
		// 2-name
		// 3-contents
		// 4-arrows
		// 5-chickens
		// 6-difficulty
		// 7-tileset
		// 8-rating
		// 9-number of votes
		private var specArray:Array;
		private var hoverFilter:GlowFilter;
		
		public function PuzzleListing(_title:String, _author:String, _rating:int, spec:String, x:int, y:int)
		{
			super();
			bg = new FlxSprite();
			bg.makeGraphic(LISTING_WIDTH, LISTING_HEIGHT, 0x000000, true);
			bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x000000);
			bg.x = x;
			bg.y = y;
			add(bg);
			
			specArray = spec.split(":");
			
			titleText = new FlxText(x + 5, y + 5, LISTING_WIDTH/2, _title, false);
			titleText.setFormat("Courier New", 20, 0xFFFF00);
			add(titleText);
			
			var text:String;
			if (Elemental.MODE == Elemental.NORMAL_MODE) {
				_author = _author.substr(4, _author.length); // snip off the "cgs_" bit.
				text = "By user " + _author + " | " + specArray[9] + " votes";
			} else if (Elemental.MODE == Elemental.FACEBOOK_MODE) {
				text = specArray[11] + " plays, " + specArray[9] + " votes";	
			}
			
			authorText = new FlxText(titleText.x, titleText.y + titleText.height, LISTING_WIDTH, text, false);
			authorText.setFormat("Courier New", 16, 0xFFFFFF);
			add(authorText);
			
			drawStars(_rating);
			hoverFilter = new GlowFilter(0xFFFFFF, 0.6, 10, 10, 3);
		}
		
		public override function update():void {
			if (bg.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				if (!overlap) {
					overlap = true;
					bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x222222);
				}
			} else if (overlap) {
				overlap = false;
				bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x000000);
			}
			
			if (FlxG.mouse.justPressed() && overlap) {
				startPuzzle();
			}
		}
		
		public function startPuzzle():void {
			var arrows:Array = String(specArray[4]).split(",");
			for (var i:int = 0; i < 4; i++) {
				arrows[i] = int(arrows[i]);
			}
			
			var chickens:Array = String(specArray[5]).split(",");
			Elemental.Log.logPlayerMapPlayed(specArray[0]);
			var ps:PuzzleSpecification = new PuzzleSpecification(specArray[3], specArray[7], arrows, chickens, specArray[0]);
			FlxG.switchState(new PuzzlePlayingState(ps));
		}
		
		/**
		 * Draws the 5 stars
		 */
		private function drawStars(rating:int):void {
			var xPos:int = (bg.x + bg.width) - (30*5) - 10;
			for (var i:int = 0; i < 5; i++) {
				var star:Class = (i + 1 <= rating) ? Res.StarFull : Res.StarEmpty;
				add(new FlxSprite(xPos, bg.y + 10, star));
				xPos += 30;
			}
		}
	}
}